#include <iostream>
using namespace std;

void StudyDynamicMemory()
{
  // 单个变量 动态内存
  double *pvalue = NULL; // 初始化为 null 的指针
  pvalue = new double;   // 为变量请求内存

  *pvalue = 29494.99; // 在分配的地址存储值
  cout << "Value of pvalue : " << *pvalue << endl;

  delete pvalue; // 释放内存

  //动态一维数组
  // 动态分配,数组长度为 m
  int *array = new int[5];
  for (int i = 0; i < 5; i++)
  {
    *(array + i) = i;
    cout << "array[" << i << "] = " << *(array + i)<<endl;
  }

  //释放内存
  delete[] array;

  // 动态二维数组
  int **p2;
  int i, j; //p[4][8]
  //开始分配4行8列的二维数据
  p2 = new int *[4];
  for (i = 0; i < 4; i++)
  {
    p2[i] = new int[8];
  }

  for (i = 0; i < 4; i++)
  {
    for (j = 0; j < 8; j++)
    {
      p2[i][j] = j * i;
    }
  }
  //打印数据
  for (i = 0; i < 4; i++)
  {
    for (j = 0; j < 8; j++)
    {
      if (j == 0)
        cout << endl;
      cout << p2[i][j] << "\t";
    }
  }
  //开始释放申请的堆
  for (i = 0; i < 4; i++)
  {
    delete[] p2[i];
  }
  delete[] p2;

  // 动态三维数组
  int k; // p[2][3][4]

  int ***p3;
  p3 = new int **[2];
  for (i = 0; i < 2; i++)
  {
    p3[i] = new int *[3];
    for (j = 0; j < 3; j++)
      p3[i][j] = new int[4];
  }

  //输出 p[i][j][k] 三维数据
  for (i = 0; i < 2; i++)
  {
    for (j = 0; j < 3; j++)
    {
      for (k = 0; k < 4; k++)
      {
        p3[i][j][k] = i + j + k;
        cout << p3[i][j][k] << " ";
      }
      cout << endl;
    }
    cout << endl;
  }

  // 释放内存
  for (i = 0; i < 2; i++)
  {
    for (j = 0; j < 3; j++)
    {
      delete[] p3[i][j];
    }
  }
  for (i = 0; i < 2; i++)
  {
    delete[] p3[i];
  }
  delete[] p3;
}

int main(int argc, char const *argv[])
{
  StudyDynamicMemory();
  return 0;
}
